/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.apps.utils;

import com.apps.beans.AccountBean;
import com.apps.beans.EnvironmentBean;
import com.apps.beans.SettingsBean;
import com.apps.controllers.AccountSetupManager;
import com.apps.controllers.ShellController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author ARIJIT PAL
 */
public class StartupHelper {

    private static ArrayList<ShellController> shellList;
    private static SettingsBean settings;
    private static Map<String, ShellController> shellMap;
    private static StatusChecker stsCk;

    public static StatusChecker getStatusChecker() {
        return stsCk;
    }

    public static void setStatusChecker(StatusChecker stsCk) {
        StartupHelper.stsCk = stsCk;
    }

    public static ArrayList<ShellController> getShellList() {
        /*shellList = new ArrayList<ShellController>();
        shellList.add(new ShellController("22", "abcd1", "22", "ap", "ap"));
        shellList.add(new ShellController("22", "abcd2", "22", "ap", "ap"));*/
        try{System.out.println("ShellList : "+shellList.size());}catch(Exception ex){}
        return shellList;
    }

    public static Map<String, ShellController> getShellMap() {
        return shellMap;
    }

    /*
     * apps.system.tray
     * apps.env.code
     * apps.env.setup.timing
     * apps.result.path
     * apps.result.opendir
     *
     */
    public static void loadSettings(){
        settings = new SettingsBean();
        SystemProperties.loadProperties();
        String temp = "";
        temp = SystemProperties.getProperties("apps.system.tray", "");
        if(temp.equals(""))
            SystemProperties.setProperties("apps.system.tray", "true");
        
        temp = SystemProperties.getProperties("apps.env.code", "");
        if(temp.equals(""))
            SystemProperties.setProperties("apps.env.code", "");

        temp = SystemProperties.getProperties("apps.env.setup.timing", "");
        if(temp.equals(""))
            SystemProperties.setProperties("apps.env.setup.timing", "2");

        temp = SystemProperties.getProperties("apps.result.path", "");
        if(temp.equals(""))
            SystemProperties.setProperties("apps.result.path", "");

        temp = SystemProperties.getProperties("apps.result.opendir", "");
        if(temp.equals(""))
            SystemProperties.setProperties("apps.result.opendir", "true");
        
        SystemProperties.saveProperties();
        settings.setApps_system_tray(SystemProperties.getProperties("apps.system.tray", ""));
        settings.setApps_env_code(SystemProperties.getProperties("apps.env.code", ""));
        settings.setApps_env_setup_timing(SystemProperties.getProperties("apps.env.setup.timing", ""));
        settings.setApps_result_path(SystemProperties.getProperties("apps.result.path", ""));
        settings.setApps_result_opendir(SystemProperties.getProperties("apps.result.opendir", ""));

    }

    public static SettingsBean getSettings() {
        loadSettings();
        return settings;
    }

    public static void setSettings(SettingsBean settings) {
        StartupHelper.settings = settings;
        SystemProperties.setProperties("apps.system.tray", settings.getApps_system_tray());
	SystemProperties.setProperties("apps.env.code", settings.getApps_env_code());
	SystemProperties.setProperties("apps.env.setup.timing", settings.getApps_env_setup_timing());
	SystemProperties.setProperties("apps.result.path", settings.getApps_result_path());
	SystemProperties.setProperties("apps.result.opendir", settings.getApps_result_opendir());

        SystemProperties.saveProperties();
    }

    public static void openAllShell(ArrayList<AccountBean> accList){
        shellList = new ArrayList<ShellController>();
        shellMap = new HashMap<String, ShellController>();

        for(AccountBean bean : accList){
            ShellController shellController = new ShellController(
                    bean.getCode(),
                    bean.getHost(), bean.getPort(), 
                    bean.getUserName(), bean.getPassword());
            shellList.add(shellController);
            shellMap.put(bean.getCode(), shellController);
            Thread th = new Thread(new ShellOpener(shellController));
            th.start();
        }
        System.out.println("OPEN_SHELL:SHELL_LIST_SIZE:"+shellList.size());
        System.out.println("OPEN_SHELL:SHELL_MAP_SIZE:"+shellMap.size());
    }

    public static void closeAllShell(){
        if(shellList!=null){
            for(ShellController shell : shellList){
                shell.closeShell();
            }
        }
        shellList = new ArrayList<ShellController>();
        shellMap = new HashMap<String, ShellController>();
    }

    public static boolean doEnvStartup(String envCode){
        System.out.println("ENV_STARTUP:START");
        try {
            if (!envCode.equals("")) {
                ArrayList<EnvironmentBean> envList = AccountSetupManager.getEnvList();
                for (EnvironmentBean bean : envList) {
                    if (bean.getCode().equalsIgnoreCase(envCode)) {
                        ArrayList<AccountBean> accList = AccountSetupManager.getAccList(envCode);
                        System.out.println("ENV_STARTUP:ACC_LIST_SIZE:"+accList.size());
                        if (accList.size() > 0) {
                            if (SystemProperties.getProperties("apps.env.setup.timing", "2").equalsIgnoreCase("2")) {
                                StartupHelper.openAllShell(accList);
                            }
                        }
                    }
                }
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public static ArrayList<AccountBean> getAccList(String envCode){
        try {
            if (!envCode.equals("")) {
                ArrayList<EnvironmentBean> envList = AccountSetupManager.getEnvList();
                for (EnvironmentBean bean : envList) {
                    if (bean.getCode().equalsIgnoreCase(envCode)) {
                        ArrayList<AccountBean> accList = AccountSetupManager.getAccList(envCode);
                        return accList;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
